package gui;

import java.awt.Point;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import algorythm.Algorythm;
import algorythm.Piece;
import algorythm.Piece.Color;
import algorythm.Piece.PieceType;

@SuppressWarnings("serial")
public class Board extends JFrame {
	private JLabel  board;
	private PieceRepresentation
	whiteKing,
	whiteQueen,
	whiteBishopL,
	whiteHorseL,
	whiteTowerL,
	whiteBishopR,
	whiteHorseR,
	whiteTowerR,
	whitePawnA,
	whitePawnB,
	whitePawnC,
	whitePawnD,
	whitePawnE,
	whitePawnF,
	whitePawnG,
	whitePawnH,
	blackTowerL,
	blackHorseL,
	blackBishopL,
	blackQueen,
	blackKing,
	blackBishopR,
	blackHorseR,
	blackTowerR,
	blackPawnA,
	blackPawnB,
	blackPawnC,
	blackPawnD,
	blackPawnE,
	blackPawnF,
	blackPawnG,
	blackPawnH;
	private HashMap<Point, Piece> boardData;
	private HashMap<Piece, PieceRepresentation> representationForPiece;
	private Color playerColor;
	private List<Point> posiblesMoves;
	static List<Polygon> columns = new ArrayList<Polygon>(),
			rows = new ArrayList<Polygon>();

	static {

		columns.add(new Polygon((new int[]{94,165,134,52}), (new int[]{57,57,579,579}), 4));
		columns.add(new Polygon((new int[]{165,235,216,135}), (new int[]{57,57,579,579}), 4));

		columns.add(new Polygon((new int[]{235,306,296,216}), (new int[]{57,57,579,579}), 4));
		columns.add(new Polygon((new int[]{306,377,377,296}), (new int[]{57,57,579,579}), 4));

		columns.add(new Polygon((new int[]{377,447,456,377}), (new int[]{57,57,579,579}), 4));
		columns.add(new Polygon((new int[]{447,516,537,456}), (new int[]{57,57,579,579}), 4));

		columns.add(new Polygon((new int[]{516,587,618,537}), (new int[]{57,57,579,579}), 4));
		columns.add(new Polygon((new int[]{587,658,698,618}), (new int[]{57,57,579,579}), 4));

		rows.add(new Polygon((new int[]{94,658,662,89}), (new int[]{57,57,115,115}), 4));
		rows.add(new Polygon((new int[]{89,662,667,86}), (new int[]{114,114,175,175}), 4));

		rows.add(new Polygon((new int[]{86,665,673,80}), (new int[]{176,176,237,237}), 4));
		rows.add(new Polygon((new int[]{80,673,677,77}), (new int[]{237,237,300,300}), 4));

		rows.add(new Polygon((new int[]{77,677,682,71}), (new int[]{300,300,367,367}), 4));
		rows.add(new Polygon((new int[]{71,682,688,64}), (new int[]{367,367,435,435}), 4));

		rows.add(new Polygon((new int[]{64,688,693,60}), (new int[]{435,435,506,506}), 4));
		rows.add(new Polygon((new int[]{60,693,697,55}), (new int[]{506,506,579,579}), 4));
	}

	Point originalPosition;
	
	public Board() 
	{
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		boardData = new HashMap<Point, Piece>();
		representationForPiece = new HashMap<Piece, PieceRepresentation>();
		Color[] colors = {Color.WHITE, Color.BLACK}; 
		playerColor = (Color) JOptionPane.showInputDialog((JFrame) null, "Select your color", "Which Color you choose?", JOptionPane.QUESTION_MESSAGE, null, colors, colors[0]);
		if (playerColor == null) {
			System.exit(0);
		}
		setUndecorated(true);
		setBackground(new java.awt.Color(0, 0, 0, 0));
		this.getContentPane().setBackground(new java.awt.Color(0, 0, 0, 0));
		setSize(754, 693);
		getContentPane().setLayout(null);
		getContentPane().add(getBoard());
		addPicesToBoard();
		setLocationRelativeTo(null);
		if (playerColor == Color.BLACK) {
			Object[] nextMove = Algorythm.getBestMove2(boardData,playerColor.getEnemyColor());
			locatePieceAt(representationForPiece.get((Piece)nextMove[1]), (Point)nextMove[0]);
			boardData.remove(((Piece)(nextMove[1])).getPosition());
			boardData.put((Point)nextMove[0],(Piece)nextMove[1]);
			((Piece)(nextMove[1])).setPosition((Point)nextMove[0]);
		}
	}

	public static void main(String[] args) 
	{
		new Board().setVisible(true); 
	}

	private void addPicesToBoard() 
	{
		board.add(getWhiteTowerL());
		board.add(getWhiteHorseL());
		board.add(getWhiteBishopL());
		board.add(getLblWhiteKing());
		board.add(getWhiteQueen());
		board.add(getWhiteBishopR());
		board.add(getWhiteHorseR());
		board.add(getWhiteTowerR());
		board.add(getWhitePawnA());
		board.add(getWhitePawnB());
		board.add(getWhitePawnC());
		board.add(getWhitePawnD());
		board.add(getWhitePawnE());
		board.add(getWhitePawnF());
		board.add(getWhitePawnG());
		board.add(getWhitePawnH());

		board.add(getBlackPawnA());
		board.add(getBlackPawnB());
		board.add(getBlackPawnC());
		board.add(getBlackPawnD());
		board.add(getBlackPawnE());
		board.add(getBlackPawnF());
		board.add(getBlackPawnG());
		board.add(getBlackPawnH());
		board.add(getBlackTowerL());
		board.add(getBlackHorseL());
		board.add(getBlackBishopL());
		board.add(getBlackQueen());
		board.add(getBlackKing());
		board.add(getBlackBishopR());
		board.add(getBlackHorseR());
		board.add(getBlackTowerR());
	}

	/*board.add(getWhiteTowerL());
	board.add(getWhiteHorseL());
	board.add(getWhiteBishopL());
	board.add(getLblWhiteKing());
	board.add(getWhiteQueen());
	board.add(getWhiteBishopR());
	board.add(getWhiteHorseR());
	board.add(getWhiteTowerR());
	board.add(getWhitePawnA());
	board.add(getWhitePawnB());
	board.add(getWhitePawnC());
	board.add(getWhitePawnD());
	board.add(getWhitePawnE());
	board.add(getWhitePawnF());
	board.add(getWhitePawnG());
	board.add(getWhitePawnH());

	board.add(getBlacPawnA());
	board.add(getBlacPawnB());
	board.add(getBlackPawnC());
	board.add(getBlackPawnD());
	board.add(getLblBlackPawnE());
	board.add(getBlackPawnF());
	board.add(getBlackPawnG());
	board.add(getBlackPawnH());
	board.add(getBlackTowerL());
	board.add(getBlackHorseL());
	board.add(getBlackBishopL());
	board.add(getBlackQueen());
	board.add(getBlackKing());
	board.add(getLblBlackBishopR());
	board.add(getBlackHorseR());
	board.add(getBlackTowerR());*/

	private JLabel getBoard() 
	{
		if (board == null) 
		{
			board = new JLabel(new ImageIcon(this.getClass().getResource("/images/Board.png")));
			board.setBounds(0, 0, 753, 665);
		}
		return board;
	}

	private JLabel getWhiteTowerL() 
	{
		if (whiteTowerL == null) 
		{
			Piece p = new Piece(new Point(0, 7), PieceType.TOWER, Color.WHITE);
			whiteTowerL = new PieceRepresentation(p);
			whiteTowerL.setBounds(70, 490, 57, 81);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteTowerL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteTowerL);
		}
		return whiteTowerL;
	}

	private JLabel getWhiteHorseL() 
	{
		if (whiteHorseL == null) 
		{
			Piece p = new Piece(new Point(1, 7), PieceType.KNIGHT, Color.WHITE);
			whiteHorseL = new PieceRepresentation(p);
			whiteHorseL.setBounds(150, 475, 58, 98);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteHorseL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteHorseL);
		}
		return whiteHorseL;
	}

	private JLabel getWhiteBishopL() 
	{
		if (whiteBishopL == null) {
			Piece p = new Piece(new Point(2, 7), PieceType.BISHOP, Color.WHITE);
			whiteBishopL = new PieceRepresentation(p);
			whiteBishopL.setBounds(231, 486, 56, 85);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteBishopL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteTowerL);
		}
		return whiteBishopL;
	}

	private JLabel getLblWhiteKing() 
	{
		if (whiteKing == null) 
		{
			Piece p = new Piece(new Point(4, 7), PieceType.KING, Color.WHITE);
			whiteKing = new PieceRepresentation(p);
			whiteKing.setBounds(382, 471, 69, 105);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteKing);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteKing);
		}
		return whiteKing;
	}

	private JLabel getWhiteQueen() 
	{
		if (whiteQueen == null) 
		{
			Piece p = new Piece(new Point(3, 7), PieceType.QUEEN, Color.WHITE);
			whiteQueen = new PieceRepresentation(p);
			whiteQueen.setBounds(304, 469, 67, 105);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteQueen);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteQueen);
		}
		return whiteQueen;
	}

	private JLabel getWhiteBishopR() {
		
		if (whiteBishopR == null) 
		{
			Piece p = new Piece(new Point(5, 7), PieceType.BISHOP, Color.WHITE);
			whiteBishopR = new PieceRepresentation(p);
			whiteBishopR.setBounds(470, 489, 56, 85);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteBishopR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteBishopR);
		}
		return whiteBishopR;
	}

	private JLabel getWhiteHorseR() 
	{
		if (whiteHorseR == null) 
		{
			Piece p = new Piece(new Point(6, 7), PieceType.KNIGHT, Color.WHITE);
			whiteHorseR = new PieceRepresentation(p);
			whiteHorseR.setBounds(549, 477, 58, 98);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteHorseR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteHorseR);
		}
		return whiteHorseR;
	}

	private JLabel getWhiteTowerR() 
	{
		if (whiteTowerR == null) 
		{
			Piece p = new Piece(new Point(7, 7), PieceType.TOWER, Color.WHITE);
			whiteTowerR = new PieceRepresentation(p);
			whiteTowerR.setBounds(631, 489, 57, 81);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whiteTowerR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whiteTowerR);
		}
		return whiteTowerR;
	}

	private JLabel getWhitePawnA() 
	{
		if (whitePawnA == null) 
		{
			Piece p = new Piece(new Point(0, 6), PieceType.PAWN, Color.WHITE);
			whitePawnA = new PieceRepresentation(p);
			whitePawnA.setBounds(72, 426, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnA);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnA);
		}
		return whitePawnA;
	}

	private JLabel getWhitePawnB() 
	{
		if (whitePawnB == null) 
		{
			Piece p = new Piece(new Point(1, 6), PieceType.PAWN, Color.WHITE);
			whitePawnB = new PieceRepresentation(p);
			whitePawnB.setBounds(151, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnB);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnB);
		}
		return whitePawnB;
	}

	private JLabel getWhitePawnC() 
	{
		if (whitePawnC == null) 
		{
			Piece p = new Piece(new Point(2, 6), PieceType.PAWN, Color.WHITE);
			whitePawnC = new PieceRepresentation(p);
			whitePawnC.setBounds(230, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnC);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnC);
		}
		return whitePawnC;
	}

	private JLabel getWhitePawnD() 
	{
		if (whitePawnD == null) 
		{
			Piece p = new Piece(new Point(3, 6), PieceType.PAWN, Color.WHITE);
			whitePawnD = new PieceRepresentation(p);
			whitePawnD.setBounds(309, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnD);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnD);
		}
		return whitePawnD;
	}

	private JLabel getWhitePawnE() 
	{
		if (whitePawnE == null) 
		{
			Piece p = new Piece(new Point(4, 6), PieceType.PAWN, Color.WHITE);
			whitePawnE = new PieceRepresentation(p);
			whitePawnE.setBounds(386, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnE);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnE);
		}
		return whitePawnE;
	}

	private JLabel getWhitePawnF() 
	{
		if (whitePawnF == null) 
		{
			Piece p = new Piece(new Point(5, 6), PieceType.PAWN, Color.WHITE);
			whitePawnF = new PieceRepresentation(p);
			whitePawnF.setBounds(459, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnF);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnF);
		}
		return whitePawnF;
	}

	private JLabel getWhitePawnG() 
	{
		if (whitePawnG == null) 
		{
			Piece p = new Piece(new Point(6, 6), PieceType.PAWN, Color.WHITE);
			whitePawnG = new PieceRepresentation(p);
			whitePawnG.setBounds(546, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnG);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnG);
		}
		return whitePawnG;
	}

	private JLabel getWhitePawnH() 
	{
		if (whitePawnH == null) 
		{
			Piece p = new Piece(new Point(7, 6), PieceType.PAWN, Color.WHITE);
			whitePawnH = new PieceRepresentation(p);
			whitePawnH.setBounds(622, 425, 61, 71);
			if (playerColor == Color.WHITE) {
				addPiceDraggableCapabilities(whitePawnH);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, whitePawnH);
		}
		return whitePawnH;
	}

	private JLabel getBlackTowerL() 
	{
		if (blackTowerL == null) 
		{
			Piece p = new Piece(new Point(0, 0), PieceType.TOWER, Color.BLACK);
			blackTowerL = new PieceRepresentation(p);
			blackTowerL.setBounds(100, 27, 58, 83);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackTowerL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackTowerL);
		}
		return blackTowerL;
	}

	private JLabel getBlackHorseL() 
	{
		if (blackHorseL == null) 
		{
			Piece p = new Piece(new Point(1, 0), PieceType.KNIGHT, Color.BLACK);
			blackHorseL = new PieceRepresentation(p);
			blackHorseL.setBounds(170, 6, 58, 100);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackHorseL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackHorseL);
		}
		return blackHorseL;
	}

	private JLabel getBlackBishopL() 
	{
		if (blackBishopL == null) 
		{
			Piece p = new Piece(new Point(2, 0), PieceType.BISHOP, Color.BLACK);
			blackBishopL = new PieceRepresentation(p);
			blackBishopL.setBounds(243, 21, 56, 85);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackBishopL);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackBishopL);
		}
		return blackBishopL;
	}

	private JLabel getBlackQueen() 
	{
		if (blackQueen == null) 
		{
			Piece p = new Piece(new Point(3, 0), PieceType.QUEEN, Color.BLACK);
			blackQueen = new PieceRepresentation(p);
			blackQueen.setBounds(309, 6, 67, 105);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackQueen);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackQueen);
		}
		return blackQueen;
	}

	private JLabel getBlackKing() 
	{
		if (blackKing == null) 
		{
			Piece p = new Piece(new Point(4, 0), PieceType.KING, Color.BLACK);
			blackKing = new PieceRepresentation(p);
			blackKing.setBounds(378, 6, 69, 107);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackKing);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackKing);
		}
		return blackKing;
	}

	private JLabel getBlackBishopR() 
	{
		if (blackBishopR == null) 
		{
			Piece p = new Piece(new Point(5, 0), PieceType.BISHOP, Color.BLACK);
			blackBishopR = new PieceRepresentation(p);
			blackBishopR.setBounds(455, 21, 56, 85);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackBishopR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackBishopR);
		}
		return blackBishopR;
	}

	private JLabel getBlackHorseR() 
	{
		if (blackHorseR == null) 
		{
			Piece p = new Piece(new Point(6, 0), PieceType.KNIGHT, Color.BLACK);
			blackHorseR = new PieceRepresentation(p);
			blackHorseR.setBounds(528, 6, 58, 100);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackHorseR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackHorseR);
		}
		return blackHorseR;
	}

	private JLabel getBlackTowerR() 
	{
		if (blackTowerR == null) 
		{
			Piece p = new Piece(new Point(7, 0), PieceType.TOWER, Color.BLACK);
			blackTowerR = new PieceRepresentation(p);
			blackTowerR.setBounds(598, 21, 58, 83);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackTowerR);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackTowerR);
		}
		return blackTowerR;
	}

	private JLabel getBlackPawnA() 
	{
		if (blackPawnA == null) 
		{
			Piece p = new Piece(new Point(0, 1), PieceType.PAWN, Color.BLACK);
			blackPawnA = new PieceRepresentation(p);
			blackPawnA.setBounds(100, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnA);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnA);
		}
		return blackPawnA;
	}

	private JLabel getBlackPawnB() 
	{
		if (blackPawnB == null) 
		{
			Piece p = new Piece(new Point(1, 1), PieceType.PAWN, Color.BLACK);
			blackPawnB = new PieceRepresentation(p);
			blackPawnB.setBounds(175, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnB);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnB);
		}
		return blackPawnB;
	}

	private JLabel getBlackPawnC() 
	{
		if (blackPawnC == null) 
		{
			Piece p = new Piece(new Point(2, 1), PieceType.PAWN, Color.BLACK);
			blackPawnC = new PieceRepresentation(p);
			blackPawnC.setBounds(247, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnC);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnC);
		}
		return blackPawnC;
	}

	private JLabel getBlackPawnD() 
	{
		if (blackPawnD == null) 
		{
			Piece p = new Piece(new Point(3, 1), PieceType.PAWN, Color.BLACK);
			blackPawnD = new PieceRepresentation(p);
			blackPawnD.setBounds(319, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnD);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnD);
		}
		return blackPawnD;
	}

	private JLabel getBlackPawnE() 
	{
		if (blackPawnE == null) 
		{
			Piece p = new Piece(new Point(4, 1), PieceType.PAWN, Color.BLACK);
			blackPawnE = new PieceRepresentation(p);
			blackPawnE.setBounds(392, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnE);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnE);
		}
		return blackPawnE;
	}

	private JLabel getBlackPawnF() 
	{
		if (blackPawnF == null) 
		{
			Piece p = new Piece(new Point(5, 1), PieceType.PAWN, Color.BLACK);
			blackPawnF = new PieceRepresentation(p);
			blackPawnF.setBounds(463, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnF);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnF);
		}
		return blackPawnF;
	}

	private JLabel getBlackPawnG() 
	{
		if (blackPawnG == null) 
		{
			Piece p = new Piece(new Point(6, 1), PieceType.PAWN, Color.BLACK);
			blackPawnG = new PieceRepresentation(p);
			blackPawnG.setBounds(536, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnG);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnG);
		}
		return blackPawnG;
	}

	private JLabel getBlackPawnH() 
	{
		if (blackPawnH == null) 
		{
			Piece p = new Piece(new Point(7, 1), PieceType.PAWN, Color.BLACK);
			blackPawnH = new PieceRepresentation(p);
			blackPawnH.setBounds(608, 91, 46, 72);
			if (playerColor == Color.BLACK) {
				addPiceDraggableCapabilities(blackPawnH);
			}
			boardData.put(p.getPosition(), p);
			representationForPiece.put(p, blackPawnH);
		}
		return blackPawnH;
	}

	private void addPiceDraggableCapabilities(final PieceRepresentation pieceRepresentation) 
	{
		pieceRepresentation.addMouseMotionListener(new MouseMotionListener() 
		{

			private Point offsets = new Point();

			@Override
			public void mouseMoved(MouseEvent e) 
			{
				offsets.x = e.getX();
				offsets.y = e.getY();
			}

			@Override
			public void mouseDragged(MouseEvent e) 
			{
				//System.out.println(pieceRelatedToImage.get(label));
				pieceRepresentation.setLocation(pieceRepresentation.getLocation().x+e.getX()-offsets.x, pieceRepresentation.getLocation().y+e.getY()-offsets.y);
			}

		});
		pieceRepresentation.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseReleased(MouseEvent e) 
			{
				System.out.println("Inicio movimiento *******************************");
				Piece pieceMoved = pieceRepresentation.getRepresentingPiece();
				posiblesMoves = pieceMoved.possibleMoves(boardData, pieceMoved,pieceMoved.getPosition());
				Point coordinate = coordiateToPosition(new Point(pieceRepresentation.getX() + (pieceRepresentation.getWidth()/2), pieceRepresentation.getY() + (int)(pieceRepresentation.getHeight() * .75)));
				if (pieceMoved.getType() == PieceType.PAWN) {
					if ((playerColor == Color.BLACK && coordinate.y == 7) || (playerColor == Color.WHITE && coordinate.y == 0)) {
						PieceType newType = reselectPawn();
						pieceMoved.changeTypeOfPiece(newType);
						pieceRepresentation.setImageForType(newType);
						pieceRepresentation.repaint();
					}
				}
				if (posiblesMoves.size() == 0) 
				{
					locatePieceAt(pieceRepresentation, pieceRepresentation.getRepresentingPiece().getPosition());
					return;
				}
				if (posiblesMoves.contains(coordinate)) 
				{
					locatePieceAt(pieceRepresentation, coordinate);
					if (boardData.containsKey(coordinate)) 
					{
						board.remove(representationForPiece.get(boardData.get(coordinate)));
						boardData.remove(coordinate);
					}
					boardData.remove(pieceRepresentation.getRepresentingPiece().getPosition());
					boardData.put(coordinate, pieceRepresentation.getRepresentingPiece());
					pieceRepresentation.getRepresentingPiece().setPosition(coordinate);
					Object[] nextMove = Algorythm.getBestMove2(boardData,playerColor.getEnemyColor());
					if (((Piece)(nextMove[1])).getType() == PieceType.PAWN) {
						if ((((Piece)(nextMove[1])).getColor() == Color.BLACK && ((Point)nextMove[0]).y == 7) || ( (((Piece)(nextMove[1]))).getColor() == Color.WHITE && ((Point)nextMove[0]).y == 0)) {
							pieceMoved.changeTypeOfPiece(PieceType.QUEEN);
							pieceRepresentation.setImageForType(PieceType.QUEEN);
							pieceRepresentation.repaint();
						}
					}
					if (boardData.containsKey((Point)nextMove[0])) 
					{
						board.remove(representationForPiece.get(boardData.get(coordinate)));
					}
					locatePieceAt(representationForPiece.get((Piece)nextMove[1]), (Point)nextMove[0]);
					boardData.remove(((Piece)(nextMove[1])).getPosition());
					boardData.put((Point)nextMove[0],(Piece)nextMove[1]);
					((Piece)(nextMove[1])).setPosition((Point)nextMove[0]);
					
				} 
				else 
				{
					locatePieceAt(pieceRepresentation, pieceRepresentation.getRepresentingPiece().getPosition());
				}
				System.out.println("Fin movimiento *******************************");
			}
			
		});
		
	}
	
	private PieceType reselectPawn() {
		PieceType[] types = {PieceType.QUEEN, PieceType.TOWER, PieceType.BISHOP, PieceType.KNIGHT};
		return (PieceType) JOptionPane.showInputDialog((JFrame) null, "Select your color", "Which Color you choose?", JOptionPane.QUESTION_MESSAGE, null, types, types[0]);
	}

	private void locatePieceAt(JLabel pieceToLocate,Point coordinatesAtBoard) 
	{
		Polygon xPolygon = columns.get(coordinatesAtBoard.x),
				yPolygon = rows.get(coordinatesAtBoard.y);
		pieceToLocate.setLocation(
				xPolygon.xpoints[0] - (int)((((float)xPolygon.xpoints[0] - (float)xPolygon.xpoints[3]) / 8f) * coordinatesAtBoard.y) + 5, 
				yPolygon.ypoints[2]-10-pieceToLocate.getHeight());
	}
	
	private Point coordiateToPosition(Point actualCoordinate) 
	{
		Point coordinates = new Point();

		for (Polygon polygon : columns) 
		{
			if (polygon.contains(actualCoordinate)) 
			{
				coordinates.x = columns.indexOf(polygon);
			}
		}
		for (Polygon polygon : rows) 
		{
			if (polygon.contains(actualCoordinate)) 
			{
				coordinates.y = rows.indexOf(polygon);
			}
		}
		return coordinates;
	}

}

//TODO: evitar uqe las piezas que queramos no se muevan || 